package com.lhs.cachefactory;

import com.lhs.aopcache.Cache;
import com.lhs.aopcache.CacheManager;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

public class CacheFactoryBean implements FactoryBean, InitializingBean {

    private String cacheRegion;

    private Cache cache;

    private CacheManager cacheManager;

    public Object getObject() throws Exception {
        return cache;
    }

    public Class getObjectType() {
        return Cache.class;
    }

    public boolean isSingleton() {
        return true;
    }

    public void afterPropertiesSet() throws Exception {
        if (cache == null) {
            cache = cacheManager.createCache(cacheRegion);
        }
    }

    public String getCacheRegion() {
        return cacheRegion;
    }

    public void setCacheRegion(String cacheRegion) {
        this.cacheRegion = cacheRegion;
    }

    public CacheManager getCacheManager() {
        return cacheManager;
    }

    public void setCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }
}
